package qq.service;

import org.apache.ibatis.session.SqlSession;
import qq.bean.SignInDate;
import qq.bean.User;
import qq.mapper.SignInMapper;
import qq.mapper.UserMapper;
import qq.tool.MybatisTool;

import java.io.IOException;
import java.util.Date;

public class SignInService {
    private static final String SUCCESS = "签到成功";
    private static final String FAIL = "你今天已经签到过了";

    public static String signIn(long id) throws IOException {
        SqlSession sqlSession = MybatisTool.getSqlSession();
        SignInMapper signInMapper = sqlSession.getMapper(SignInMapper.class);
        UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
        SignInDate signInDate1 = signInMapper.getSignInInformation(id);

        System.out.println(signInDate1+"----------------------------------------------");
        if (signInDate1==null){
            signInMapper.createSignin(id);
            userMapper.insertNewUser(id);
            sqlSession.commit();
            return SUCCESS+"当前积分为："+userMapper.getUser(id).getPoints();
        }else if (signInDate1.getSignInTime().getDay() == new Date().getDay()){
            return FAIL;
        }else {
            signInMapper.signIn(id);
            UserSevice.addPoint(id,10);
            sqlSession.commit();
            return SUCCESS+",当前积分为："+userMapper.getUser(id).getPoints();
        }
    }

    public static User viewPoint(long qq) throws IOException {
        SqlSession sqlSession = MybatisTool.getSqlSession();
        UserMapper userMapper =sqlSession.getMapper(UserMapper.class);
        return userMapper.getUser(qq);
    }
}
